package leet.code;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;

public class test {
    @Nested
    class Solution {
        public int[] topKFrequent(int[] nums, int k) {
            Map<Integer,Integer> num_fre = new HashMap<>();
            Map.Entry<Integer,Integer> kv;
            for(int i :nums){
                num_fre.put(i,num_fre.getOrDefault(i,0)+1);
            }
            PriorityQueue<Map.Entry<Integer,Integer>> frequency  = new PriorityQueue<>((a,b)-> a.getValue()-b.getValue());//<>();
            for(Map.Entry<Integer, Integer> entry : num_fre.entrySet()){

                frequency.offer(entry) ;
                if(frequency.size()>k){
                    frequency.poll();
                }
                // counter++;
            }

            int[] res = frequency.stream().mapToInt(Map.Entry::getKey).toArray();
            return res;
        }
    }
        @Test
        public void tesy() {
            Solution solution = new Solution();
            System.out.printf(Arrays.toString(solution.topKFrequent(new int[]{1, 1, 1, 2, 2, 3}, 2)));

        }
    }

